﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ProjectManagement.ServiceReference;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Microsoft.Practices.Prism.Commands;

namespace ProjectManagement
{
    public partial class MainPage : UserControl
    {
        public ObservableCollection<Project> listOfProjects;
                
        private DelegateCommand _addUserCommand;

        private string _statusIDToEdit;
        public string StatusIDToEdit 
        {
            get { return _statusIDToEdit; }
            set { _statusIDToEdit = value;} 
        }

        public User SelectedUser;

        //public static event StatusChangedEventHandler statusChanged;
        public MainPage()
        {
            InitializeComponent();
            listOfProjects = new ObservableCollection<Project>();

            ProjectsListBox.DataContext = listOfProjects;
            this.DataContext = this;
            SelectedUser = new ServiceReference.User { Name = "Рома" };

            Task t = new Task();
            t.setTaskStatus(Status1.notAssigned);

            

        }

        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < 10; ++i)
            { listOfProjects.Add(new Project()); }

            ProjectDatabaseInteractionClient svc = new ProjectDatabaseInteractionClient();

            svc.AddNewRecordCompleted += (_s, _e) =>
                {
                    MessageBox.Show(String.Format("Пользователь добавлен!"));
                };
            svc.AddNewRecordAsync();
        }

        private void somethingClicked(object sender, RoutedEventArgs e)
        {
            //statusChanged(this, new StatusChangedEventArgs(Status.draft));
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            ProjectDatabaseInteractionClient svc = new ProjectDatabaseInteractionClient();

            svc.PrintAllUsersCompleted += (_s, _e) =>
                {
                    MessageBox.Show(String.Format("Список пользователей : \n {0}", _e.Result.ToString()));
                };
            svc.PrintAllUsersAsync();

            svc.EditStatusCompleted += (_s, _e) =>
            {
                MessageBox.Show("");
            };
            svc.EditStatusAsync(1);

        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            ProjectDatabaseInteractionClient svc = new ProjectDatabaseInteractionClient();

            svc.EditStatusCompleted += (_sender, _e) =>
            {
                MessageBox.Show(String.Format("Статус изменен !"));
            };
            int stID;
            if (int.TryParse(StatusIDToEdit, out stID))
                svc.EditStatusAsync(stID);
            else svc.EditStatusAsync(1);
        }


        private void button4_Click(object sender, RoutedEventArgs e)
        {
            ProjectDatabaseInteractionClient svc = new ProjectDatabaseInteractionClient();

            svc.GetUserRolesCompleted += (_sender, _e) =>
            {
                MessageBox.Show("5");
            };

            svc.GetUserRolesAsync(3);
         
        }

        public DelegateCommand AddUserCommand
        {
            get
            {
                if (_addUserCommand == null)
                {
                    _addUserCommand = new DelegateCommand(
                        executeMethod: () =>
                        {
                            ProjectDatabaseInteractionClient svc = new ProjectDatabaseInteractionClient();

                            svc.AddUserCompleted += (_sender,  _e) =>
                                {
                                    MessageBox.Show(_e.Result);
                                };

                            svc.AddUserAsync(SelectedUser);
                        },
                        canExecuteMethod: () =>
                        {
                            return true;
                        }
                        );
                }
                return _addUserCommand;
            }
        }

        public enum SupervisorPermission {UserWantToSolveTask};
        
        #region для UseCase ##2.4.6.1
        public void askSupervisorPermission(SupervisorPermission mode, Task task, User worker, User supervisor)
        {
            switch(mode)
            {
                case SupervisorPermission.UserWantToSolveTask :
                    //sendEventToSupervisor(supervisor, worker, task, mode);
                    break;
            }
        }
        // для UseCase ##2.4.6.1
        public void addTaskToUserTaskList(Task task, User worker)
        {
            ProjectDatabaseInteractionClient svc = new ProjectDatabaseInteractionClient();

            svc.addToUserDutiesListCompleted += (_sender, _e) =>
            {
                //userTasks.Refresh();
                string response = _e.Result ? "Сделано!" : "Не удалось завершить операцию";
                MessageBox.Show(response);
            };

            //svc.addToUserDutiesListAsync(true, task.TaskID, worker.UserID, Status.adopted);
        }
        #endregion

        #region для UseCase ##2.4.6.2
        public void addProjectToUserProjectsList(Project project, User worker)
        {
            ProjectDatabaseInteractionClient svc = new ProjectDatabaseInteractionClient();

            svc.addToUserDutiesListCompleted += (_sender, _e) =>
            {
                //userTasks.Refresh();
                string response = _e.Result ? "Сделано!" : "Не удалось завершить операцию";
                MessageBox.Show(response);
            };

        //    svc.addToUserDutiesListAsync(false, project.ProjectID, worker.UserID, Status.adopted);
        }
        #endregion

        //#region для UseCase ##2.4.6.3 - 6
        //public void setProjectRealStartTime(Project proj, DateTime time)
        //{
        //    setEntityTimeProperty(false, true, proj.ProjectID, time);
        //}

        //// для UseCase ##2.4.6.3 - 4
        //public void setTaskRealStartTime(Task task, DateTime time)
        //{
        //    setEntityTimeProperty(true, true, task.TaskID, time);
        //}

        //// для UseCase ##2.4.6.5-6
        //public void setProjectRealFinishTime(Project proj, DateTime time)
        //{
        //    setEntityTimeProperty(false, false, proj.ProjectID, time);
        //}

        //// для UseCase ##2.4.6.5-6
        //public void setTaskRealFinishTime(Task task, DateTime time)
        //{
        //    setEntityTimeProperty(true, false, task.TaskID, time);
        //}

        //// для UseCase ##2.4.6.3-6
        //private void setEntityTimeProperty (bool mode1, bool mode2, int entityId, DateTime timeToFix)
        //{
        //    ProjectDatabaseInteractionClient svc = new ProjectDatabaseInteractionClient();

        //    svc.changeRealWorkTimeCompleted += (_sender, _e) =>
        //    {
                
        //        string response = _e.Result ? "Готово!" : "Не удалось завершить операцию";
        //        MessageBox.Show(response);
        //    };

        //    svc.changeRealWorkTimeAsync(mode1, mode2, entityId, timeToFix);
        //}
        //#endregion

        //#region для UseCase ##2.4.6.7 - 8
        //public void projectStatusToSolved(Project proj)
        //{
        //    entityStatusHandler(false, proj.ProjectID, Status.solved);
        //}

        //// для UseCase ##2.4.6.7
        //public void taskStatusToSolved(Task task)
        //{
        //    entityStatusHandler(true, task.TaskID, Status.solved);
        //}

        //// для UseCase ##2.4.6.7
        //public void projectStatusToVerifying(Project proj)
        //{
        //    entityStatusHandler(false, proj.ProjectID, Status.verifying);
        //}

        //// для UseCase ##2.4.6.7
        //public void taskStatusToVerifying(Task task)
        //{
        //    entityStatusHandler(true, task.TaskID, Status.verifying);
        //}

        //// для UseCase ##2.4.6.7
        //public void projectStatusToCompleted(Project proj)
        //{
        //    entityStatusHandler(false, proj.ProjectID, Status.completed);
        //}

        //// для UseCase ##2.4.6.7
        //public void taskStatusToCompleted(Task task)
        //{
        //    entityStatusHandler(true, task.TaskID, Status.completed);
        //}

        //// для UseCase ##2.4.6.8
        //public void projectStatusToPause(Project proj)
        //{
        //    entityStatusHandler(false, proj.ProjectID, Status.expect);
        //}

        //// для UseCase ##2.4.6.8
        //public void taskStatusToPause(Task task)
        //{
        //    entityStatusHandler(true, task.TaskID, Status.expect);
        //}

        //// для UseCase ##2.4.6.7 - 8
        //private void entityStatusHandler(bool entityType, int entityId, Status newStatus)
        //{
        //    ProjectDatabaseInteractionClient svc = new ProjectDatabaseInteractionClient();

        //    svc.changeEntityStatusCompleted += (_sender, _e) =>
        //    {

        //        string response = _e.Result ? "Готово!" : "Не удалось завершить операцию";
        //        MessageBox.Show(response);
        //    };

        //    svc.changeEntityStatusAsync(entityType, entityId, newStatus);
        //}
        //#endregion


    
    }
}
